(function() {
	var $ = require(L('$')), _ = require(L('_'));

	// var pui = $.layout('partial/camera_controller');
	// pui.nav.add($.ui('navbar')(false, false, true, 'Capture image'));
	// $.tap(pui.btnCamera, function() {
		// Ti.Media.takePicture();
	// });
	// var overlay = pui.view;

	var photo = {};
	photo.shoot = function() {
		Ti.Media.takePicture();
	};
	photo.take = function(callBack) {
		Ti.Media.showCamera({
			success : function(event) {
				$.mask(true, true);
				Ti.Media.hideCamera();
				callBack && callBack(event.media);
			},
			cancel : function() {
			},
			error : function(error) {
				// if (error.code == Ti.Media.NO_CAMERA)
				// alert('No camera!');
			},
			saveToPhotoGallery : true,
			allowImageEditing : true,
			//overlay : overlay,
			showControls : true,
			mediaTypes : Ti.Media.MEDIA_TYPE_PHOTO,
			autohide : true,
			//transform :
			//videoMaximumDuration : 10000,
		});
	};
	photo.upload = function(media) {
		if ($.isOnline()) {
			var pui = $.layout('popup/uploading');
			pui.progressBar.style = Ti.UI.iPhone.ProgressBarStyle.PLAIN;
			pui.progressBar.show();
			pui.win.open();
			var xhr = Ti.Network.createHTTPClient();
			xhr.onerror = function(e) {
				$.w('ERROR : ' + JSON.stringify(e));
				_.delay(function() {
					pui.win.close();
				}, 500);
			};
			xhr.onload = function() {
				$.w('# Status : ' + this.status + ' ReadyState ' + this.readyState);
				_.delay(function() {
					pui.win.close();
				}, 500);
			};
			xhr.onsendstream = function(e) {
				pui.progressBar.value = e.progress;
			};
			xhr.timeout = 120000;
			xhr.open('POST', L('url_api') + '/UploadPhoto?data={}');
			xhr.send({
				media : media
			});
		}
	};
	photo.select = function(callBack) {
		Ti.Media.openPhotoGallery({
			success : function(event) {
				if (event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
					callBack && callBack(event.media);
			},
			cancel : function() {
			},
			error : function(err) {
				$.w(err);
			},
			mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
		});
	};

	module.exports = photo;
})();
